<script>
	import {
		getAccessTokenWechat,
		getAccessTokenAli,
		getUserInfo
		
	} from 'api/api.js'

	export default {
		globalData:{
			platform:'ali',//'wx','ali'
			storeMoneyModel:undefined,//中间传递值
			isInAddOrderPage:false,//在下单页
			isLogin:false,
			webUrl:'',
			aliQrcode:'',
			fileName:''
		
		},
		onLaunch: function(e) {
			// if (uni.getSystemInfoSync().uniPlatform === 'mp-weixin'){
			// 	this.globalData.platform = 'wx'
			// }else{
			// 	if (e.query && e.query.qrCode){
			// 		var qrCode = e.query.qrCode
			// 		getApp().globalData.aliQrcode = qrCode
			// 	}
			// }
				
			// this.requestUserInfo()
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		},
		methods: {
			requestUserInfo(phoneBack){
				uni.showLoading()
				getUserInfo().then(res=>{
					if (res.data.code == 200){
						uni.hideLoading()
						var data = res.data.data
						uni.setStorageSync('hqt-mix-mobile',data.mobile)
						uni.setStorageSync('hqt-mix-id',data.id)
						if (phoneBack){
							phoneBack({code:800})
						}
						if (getApp().globalData.isLogin == false){
							getApp().globalData.isLogin = true
							uni.$emit('haveLogin')
						}
						return
					}
					if (phoneBack){
						phoneBack({code:801})
					}
					if (res.data.code == 403){
						this.hqtLogin()
					}
				}).catch(err=>{
					uni.hideLoading()
					if (phoneBack){
						phoneBack({code:801})
					}
				})
			},
			hqtLogin(callBack) {
				uni.login({
					success: (res2) => {
						this.requestLogin(callBack,res2.code)
					},
					fail: () => {
						uni.hideLoading()
						uni.showToast({
							title: "登录授权失败",
							icon: "none"
						});
						if (callBack){
							callBack({code:801})
						}
					}
				})
			},
			requestLogin(callBack,code) {
				if (uni.getSystemInfoSync().uniPlatform === 'mp-weixin') {
					// 如果是微信小程序
					getAccessTokenWechat({
						code: code
					}).then(res => {
						uni.hideLoading()
						if (res.data.code == 200) {
							var token = res.data.data.token
							uni.setStorageSync('hqt-mix-token',token)
							this.requestUserInfo()
							if (callBack){
								callBack({code:800})
							}
							return
						}
						if (callBack){
							callBack({code:801})
						}
					}).catch(err=>{
						uni.hideLoading()
						if (callBack){
							callBack({code:801})
						}
					})
				}

				if (uni.getSystemInfoSync().uniPlatform === 'mp-alipay') {
					// 如果是支付宝小程序
					getAccessTokenAli({
						authCode: code
					}).then(res => {
						uni.hideLoading()
						if (res.data.code == 200) {
							console.log('登录成功')
							var token = res.data.msg
							console.log('登录成功',token)
							uni.setStorage({
								key: 'hqt-mix-token',
								data: token
							})
							this.requestUserInfo()
							if (callBack){
								callBack({code:800})
							}
							return
						}
						// setTimeout(res0=>{
						// 	this.requestLogin('',code)
						// },2000)
						if (callBack){
							callBack({code:801})
						}
					}).catch(err=>{
						uni.hideLoading()
						if (callBack){
							callBack({code:801})
						}
						
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	
	@import "@/uni_modules/uview-ui/index.scss";
	
	page {
	  background-color: #ECECEC;
	}
	* {
	    margin: 0px;
	    padding: 0px;
	    box-sizing: border-box;
	}
	button::after { border: none }
	
	.flex-row-start{
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: flex-start;
	}
	.flex-row-center{
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
		
	}
	.flex-colume-start{
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: flex-start;
	}
	.flex-colume-center{
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
	
	.text-one-line{
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
	}
	
	.videoBox1{
	    margin-left: 10rpx;
		margin-top: 20rpx;
		width: 690rpx;
		height: 400rpx;
	}
	
</style>